package com.scau402.system1.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scau402.system1.domain.dto.WarehouseStorage;
import com.scau402.system1.domain.po.WmsCheckBoundOrderPO;
import com.scau402.system1.domain.po.WmsCheckboundOrderDetailPO;
import com.scau402.system1.domain.query.WmsCheckBoundOrderQuery;
import com.scau402.system1.domain.vo.WmsAdjustBoundOrderVO;
import com.scau402.system1.domain.vo.WmsCheckBoundOrderVO;
import com.scau402.system1.mapper.IWmsCheckboundOrderMapper;
import com.scau402.system1.mapper.IWmsCheckboundWareStorageMapper;
import com.scau402.system1.repository.IWmsCheckDetailRepository;
import com.scau402.system1.repository.IWmsCheckOrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 盘点单Repository实现类
 * @since 2024/2/3
 */
@Repository
@Slf4j
public class WmsCheckOrderRepositoryImpl implements IWmsCheckOrderRepository {
    @Autowired
    private IWmsCheckboundOrderMapper checkOrderMapper;
    @Autowired
    private IWmsCheckDetailRepository checkDetailRepo;
    @Autowired
    private IWmsCheckboundWareStorageMapper checkWareStorageMapper;

    /**
     * 插入盘点单数据
     * @param normalPO 盘点单数据PO
     */
    @Override
    public void save(WmsCheckBoundOrderPO normalPO) {
        checkOrderMapper.insert(normalPO);
    }

    /**
     * 插入盘点单数据，同时插入盘点详情数据
     * @param normalPO 盘点单数据PO
     * @param detailPO 详情数据PO
     */
    @Override
    public void save(WmsCheckBoundOrderPO normalPO, WmsCheckboundOrderDetailPO detailPO) {
        checkOrderMapper.insert(normalPO);
        checkDetailRepo.save(detailPO);
    }

    /**
     * 根据仓库id查询是否存在正在盘点对应仓库的盘点单
     * @param warehouseId 仓库id
     * @return 正在盘点的盘点单id列表
     */
    @Override
    public List<Long> findActiveOrderByWarehouseId(Long warehouseId) {
        return Optional.ofNullable(checkOrderMapper.selectByWarehouseId(warehouseId)).orElse(new ArrayList<>());
    }

    /**
     * 根据仓位id擦汗寻是否存在正在盘点对应仓位的盘点单
     * @param storageId 仓位id
     * @return 正在盘点的盘点单id列表
     */
    @Override
    public List<Long> findActiveOrderByStorageId(Long storageId) {
        return Optional.ofNullable(checkOrderMapper.selectByStorageId(storageId)).orElse(new ArrayList<>());
    }

    /**
     * 根据创建者查询盘点单
     * @param query 查询
     * @return 盘点单列表
     */
    @Override
    public List<WmsCheckBoundOrderVO> findListByCreator(WmsCheckBoundOrderQuery query) {
//        log.info("用户名：{}",userName);
        return checkOrderMapper.selectCheckBoundList(query);
    }

    /**
     * 根据盘点单id更新盘点单数据
     * @param wmsCheckBoundOrderPO 被更新的数据
     * @param checkOrderId 盘点单id
     */
    @Override
    public void updateByCheckOrderId(WmsCheckBoundOrderPO wmsCheckBoundOrderPO, Long checkOrderId) {
        checkOrderMapper.update(wmsCheckBoundOrderPO,new QueryWrapper<WmsCheckBoundOrderPO>().eq("id",checkOrderId));
    }

    /**
     * 通过盘点单id获取盘点单
     * @param checkOrderId 盘点单id
     * @return
     */
    @Override
    public WmsCheckBoundOrderPO findById(Long checkOrderId) {
        return checkOrderMapper.selectById(checkOrderId);
    }

    /**
     * 更新盘点单状态为盘点结算中
     * @param checkOrderId 盘点单Id
     */
    @Override
    public void updateToChecking(Long checkOrderId) {
        checkOrderMapper.updateToChecking(checkOrderId);
    }

    /**
     * 更新盘点单状态为盘点结算失败
     * @param checkOrderId 盘点单Id
     */
    @Override
    public void updateToFailed(Long checkOrderId) {
        checkOrderMapper.updateToCheckFailed(checkOrderId);
    }

    /**
     * 保存调整单与仓库、仓位关系
     * @param id 调整单id
     * @param targetList 仓库、仓位
     */
    @Override
    public void saveOrderWareStorageRelation(Long id, List<WarehouseStorage> targetList) {
        checkWareStorageMapper.saveOrderWareStorageRelation(id,targetList);
    }
}
